{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "increasing-model",
   "metadata": {},
   "source": [
    "Examples require an initialized GsSession and relevant entitlements. External clients need to substitute thier own client id and client secret below. Please refer to [Authentication](https://developer.gs.com/p/docs/institutional/platform/authentication/) for details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "color-legislation",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "destroyed-saying",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.data import Dataset\n",
    "import datetime as dt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "labeled-tobacco",
   "metadata": {},
   "source": [
    "The entire vol surface is published for each snap of the data, so we don’t have to pull diffs and reconstruct the surface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "accepted-jungle",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_id = 'EDRVOL_PERCENT_V1_STANDARD' # https://marquee.gs.com/s/developer/datasets/EDRVOL_PERCENT_STANDARD\n",
    "ds = Dataset(dataset_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "smoking-musical",
   "metadata": {},
   "source": [
    "Get the latest available datapoint."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "digital-korean",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>assetId</th>\n",
       "      <th>strikeReference</th>\n",
       "      <th>tenor</th>\n",
       "      <th>relativeStrike</th>\n",
       "      <th>absoluteStrike</th>\n",
       "      <th>impliedVolatility</th>\n",
       "      <th>updateTime</th>\n",
       "      <th>bbid</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>9m</td>\n",
       "      <td>1.5</td>\n",
       "      <td>6298.582757</td>\n",
       "      <td>0.154795</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        assetId strikeReference tenor  relativeStrike  \\\n",
       "date                                                                    \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward    9m             1.5   \n",
       "\n",
       "            absoluteStrike  impliedVolatility          updateTime bbid  \n",
       "date                                                                    \n",
       "2021-06-07     6298.582757           0.154795 2021-06-07 23:12:59  SPX  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "last = ds.get_data_last(as_of=dt.date.today(), bbid=['SPX'])\n",
    "last"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "twenty-relief",
   "metadata": {},
   "source": [
    "We can use this to identify the timestamp of the latest surface and then query for the full surface matching this timestamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "finnish-consultancy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>assetId</th>\n",
       "      <th>strikeReference</th>\n",
       "      <th>tenor</th>\n",
       "      <th>relativeStrike</th>\n",
       "      <th>absoluteStrike</th>\n",
       "      <th>impliedVolatility</th>\n",
       "      <th>updateTime</th>\n",
       "      <th>bbid</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.40</td>\n",
       "      <td>1668.151925</td>\n",
       "      <td>0.393684</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.50</td>\n",
       "      <td>2085.189907</td>\n",
       "      <td>0.351715</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.55</td>\n",
       "      <td>2293.708897</td>\n",
       "      <td>0.333246</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.60</td>\n",
       "      <td>2502.227888</td>\n",
       "      <td>0.315852</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.65</td>\n",
       "      <td>2710.746879</td>\n",
       "      <td>0.299288</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        assetId strikeReference tenor  relativeStrike  \\\n",
       "date                                                                    \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.40   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.50   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.55   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.60   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.65   \n",
       "\n",
       "            absoluteStrike  impliedVolatility          updateTime bbid  \n",
       "date                                                                    \n",
       "2021-06-07     1668.151925           0.393684 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2085.189907           0.351715 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2293.708897           0.333246 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2502.227888           0.315852 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2710.746879           0.299288 2021-06-07 23:12:59  SPX  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "snap_time = last.index[0].date()  # the dataframe is indexed on date\n",
    "df = ds.get_data(bbid=['SPX'], start=snap_time, end=snap_time, strikeReference='forward')\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vulnerable-pipeline",
   "metadata": {},
   "source": [
    "The dataframe can be consumed directly or can be serialized for consumption by a different process"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "growing-gibson",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(f'SPX_{snap_time}_curve.csv', 'w') as f:\n",
    "    df.to_csv(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "frozen-twenty",
   "metadata": {},
   "source": [
    "Above process can be abstracted to a function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "negative-hampton",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_latest_vol_surface(dataset, bbid, strike_reference='delta', intraday=False):\n",
    "    # Get the date/time of the most recent snap\n",
    "    as_of = dt.datetime.now() if intraday else dt.date.today()\n",
    "    last_data = dataset.get_data_last(as_of=as_of, bbid=bbid)\n",
    "\n",
    "    # Pull the surface with the date/time of the most recent snap\n",
    "    last_time = last_data.index[0] if intraday else last_data.index[0].date()\n",
    "    df = ds.get_data(bbid=bbid, start=last_time, end=last_time, strikeReference=strike_reference)\n",
    "\n",
    "    # Write latest vol surface to CSV\n",
    "    with open(f'{bbid}_{last_time}.csv', 'w') as f:\n",
    "        print(f'Writing latest vol surface for {bbid} to {bbid}_{last_time}.csv')\n",
    "        df.to_csv(f)\n",
    "\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "english-western",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing latest vol surface for SPX to SPX_2021-06-07.csv\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>assetId</th>\n",
       "      <th>strikeReference</th>\n",
       "      <th>tenor</th>\n",
       "      <th>relativeStrike</th>\n",
       "      <th>absoluteStrike</th>\n",
       "      <th>impliedVolatility</th>\n",
       "      <th>updateTime</th>\n",
       "      <th>bbid</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.40</td>\n",
       "      <td>1668.151925</td>\n",
       "      <td>0.393684</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.50</td>\n",
       "      <td>2085.189907</td>\n",
       "      <td>0.351715</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.55</td>\n",
       "      <td>2293.708897</td>\n",
       "      <td>0.333246</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.60</td>\n",
       "      <td>2502.227888</td>\n",
       "      <td>0.315852</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-07</th>\n",
       "      <td>MA4B66MW5E27U8P32SB</td>\n",
       "      <td>forward</td>\n",
       "      <td>18m</td>\n",
       "      <td>0.65</td>\n",
       "      <td>2710.746879</td>\n",
       "      <td>0.299288</td>\n",
       "      <td>2021-06-07 23:12:59</td>\n",
       "      <td>SPX</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        assetId strikeReference tenor  relativeStrike  \\\n",
       "date                                                                    \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.40   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.50   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.55   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.60   \n",
       "2021-06-07  MA4B66MW5E27U8P32SB         forward   18m            0.65   \n",
       "\n",
       "            absoluteStrike  impliedVolatility          updateTime bbid  \n",
       "date                                                                    \n",
       "2021-06-07     1668.151925           0.393684 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2085.189907           0.351715 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2293.708897           0.333246 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2502.227888           0.315852 2021-06-07 23:12:59  SPX  \n",
       "2021-06-07     2710.746879           0.299288 2021-06-07 23:12:59  SPX  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_latest_vol_surface(ds, 'SPX', strike_reference='forward').head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}